/*
	Filename	:	bjBonus.cpp
	Author(s)	:	Ray E. Bornert II
	Date		:	2002-MAY-14
	Copyright	:	(c) 2002 RealTimeGaming

	History		:
*/

//////////////
// INCLUDES //
//////////////
#include "bjbonus.h"
#include <string.h>
#include "cardset.h"

BJBonus::BJBonus()
{
	memset(this,0,sizeof(BJBonus));
}

BJBonus::~BJBonus()
{
}

void BJBonus::SetBonus
(
	char	who,			//dealer or player
	char	type,			//win or instant
	char*	pName,			//name of the bonus
	char*	pPattern,		//card pattern
	char*	pPattern2,		//card pattern 2
	int		minHands,		//min hands to qualify
	int		maxHands,		//max hands to qualify
	int		minCards,		//min cards to qualify
	int		maxCards,		//max cards to qualify
	int		minTotal,		//min total to qualify
	int		maxTotal,		//max total to qualify
	int		minNBets,		//min number of bets
	int		maxNBets,		//max number of bets
	int		bonusTotal,		//bonus added to hand score
	float	bonusPayout		//bonus payout in terms of the original bet
)
{
	strncpy(&m_name[0], pName, sizeof(m_name) );

	if (pPattern)
		strncpy(&m_pattern[0], pPattern, sizeof(m_pattern) );

	if (pPattern2)
		strncpy(&m_pattern2[0], pPattern2, sizeof(m_pattern2) );

	m_who			= who;
	m_type			= type;
	m_minHands		= minHands;
	m_maxHands		= maxHands;
	m_minCards		= minCards;
	m_maxCards		= maxCards;
	m_minTotal		= minTotal;
	m_maxTotal		= maxTotal;
	m_minNBets		= minNBets;
	m_maxNBets		= maxNBets;
	m_bonusTotal	= bonusTotal;
	m_bonusPayout	= bonusPayout;
}

char BJBonus::GetWho()
{
	return m_who;
}

char BJBonus::GetType()
{
	return m_type;
}

char* BJBonus::GetName()
{
	return (&m_name[0]);
}

int	BJBonus::GetBonusTotal()
{
	return m_bonusTotal;
}

float BJBonus::GetBonusPayout()
{
	return m_bonusPayout;
}

int BJBonus::IsPattern()
{
	return (m_pattern[0] != 0);
}

int BJBonus::IsPattern2()
{
	return (m_pattern2[0] != 0);
}

int BJBonus::IsHandQualified( char who, BJHand* pHand, int nHands, unsigned char duc )
{
	//get the number of cards in the hand
	int nCards	= pHand->getCount();

	//get the total of the hand
	int total	= pHand->GetScore();

	int nBets	= pHand->GetNBets();

	unsigned char* pCards = pHand->getCards();

	if (m_who	== who)
	if (nHands	>= m_minHands)
	if (nHands	<= m_maxHands)
	if (nCards	>= m_minCards)
	if (nCards	<= m_maxCards)
	if (total	>= m_minTotal)
	if (total	<= m_maxTotal)
	if (nBets	>= m_minNBets)
	if (nBets	<= m_maxNBets)
	{
		//pattern present
		if (IsPattern())
		if (!IsCardsInSet( pCards, nCards, (unsigned char *)&m_pattern[0] ))
			return 0;

		//pattern present DUC
		if (IsPattern2())
		{
			nCards = 1;
			pCards = &duc;

			if (!IsCardsInSet( pCards, nCards, (unsigned char *)&m_pattern2[0] ))
				return 0;
		}

		return 1;
	}
	return 0;

}

int BJBonus::IsPlayerHandQualified( BJHand* pPlayer, int nHands, unsigned char duc )
{
	return IsHandQualified( 'P', pPlayer, nHands, duc );
}

int BJBonus::IsDealerHandQualified( BJHand* pDealer, int nHands )
{
	return IsHandQualified( 'D', pDealer, nHands );
}

int BJBonus::IsDealerHandPossible( BJHand* pDealer, int nHands )
{
	return IsHandPossible( 'D', pDealer, nHands );
}

int BJBonus::IsHandPossible( char who, BJHand* pHand, int nHands, unsigned char duc )
{
	//get the number of cards in the hand
	int nCards	= pHand->getCount();

	//get the total of the hand
	int total	= pHand->GetScore();

	//get number of bets
	int nBets	= pHand->GetNBets();

	unsigned char* pCards = pHand->getCards();

	int isSoft	= pHand->IsSoft();

	//gotta be the right person
	if (m_who	!= who)
		return 0;

	//can't have too many hands
	if (nHands	> m_maxHands)
		return 0;

	//can't have too many cards
	if (nCards	> m_maxCards)
		return 0;

	//can't have too many bets
	if (nBets > m_maxNBets)
		return 0;

	if (isSoft)
	if (total-10 > m_maxTotal)
		return 0;

	if (!isSoft)
	if (total	> m_maxTotal)
		return 0;

	//pattern present
	if (IsPattern())
	if (IsCardsOutsideSet( pCards, nCards, (unsigned char *)&m_pattern[0] ))
		return 0;		

	return 1;
}
